//
//  SAHTTPSessionManager.m
//  ShopAssistant
//
//  Created by FangGeek on 2017/11/15.
//  Copyright © 2017年 hyxt. All rights reserved.
//
//DEV接口地址：https://mddev.ematong.com/el-ec-wap
//TEST接口地址：https://mdtest.ematong.com/el-ec-wap
//UAT接口地址：https://bmd.ematong.com/el-ec-wap
//PROD接口地址：https://md.ematong.com/el-ec-wap

#import "SAHTTPSessionManager.h"

#define SA_HOST_PATH @"http://bmd.ematong.com/el-ec-wap"

@interface SAHTTPSessionManager ()
@property (nonatomic, copy) NSString *host;
@property (nonatomic, copy) NSString *jsessionid;
@end

@implementation SAHTTPSessionManager
//单例工厂方法
SASINGLETON_FOR_CLASS(SAHTTPSessionManager);

-(instancetype)init
{
    if (self = [super init])
    {
        self.host = SA_HOST_PATH;
        self.requestSerializer = [AFHTTPRequestSerializer serializer];
        self.requestSerializer.timeoutInterval  = 10;
        self.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
        
    }
    return self;
}

#pragma mark - Public Method

-(void)requestWithType:(HttpRequestType)type withParaments:(id)paraments withCMD:(NSString *)url withComeBack:(requestSuccess)requestSuccess
{
    
    if (self.jsessionid.length == 0)
    {
        [self GET:[NSString stringWithFormat:@"%@/api/auth/jsessionid",self.host] parameters:paraments progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
         {
             if ([responseObject[@"status"] integerValue] == 1)
             {
                 self.jsessionid = responseObject[@"content"];
                 [self.requestSerializer setValue:self.jsessionid forHTTPHeaderField:@"Cookie"];
                 [self requestWithType:type withParaments:paraments withCMD:url withComeBack:requestSuccess];
             }
             
             
         } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
         {
             requestSuccess(nil,error);
         }];
        
        return;
    }
    
    NSString *urlString = [NSString stringWithFormat:@"%@%@",self.host,url];
    switch (type) {
            
        case HttpRequestTypeGet:
        {
            [self GET:urlString parameters:paraments progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
             {
                 requestSuccess(responseObject,nil);
                 
             } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
             {
                 requestSuccess(nil,error);
             }];
            
            break;
        }
            
        case HttpRequestTypePost:
        {
            [self POST:urlString parameters:paraments constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                requestSuccess(responseObject,nil);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                requestSuccess(nil,error);
            }];
        }
            break;
        case HttpRequestTypePostBody:
        {
            NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:paraments error:nil];
            request.timeoutInterval = 10;
            request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
            request.allHTTPHeaderFields = self.requestSerializer.HTTPRequestHeaders;
            [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody:[[paraments mj_JSONString] mj_JSONData]];
            [[self dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error)
              {
                  requestSuccess(responseObject,error);
              }] resume];
        }
            break;
        case HttpRequestTypePostData:
        {
            NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:paraments error:nil];
            request.timeoutInterval = 10;
            request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
            request.allHTTPHeaderFields = self.requestSerializer.HTTPRequestHeaders;
            [request setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
            [request setValue:@"name" forHTTPHeaderField:@"uploadFile"];
            [request setValue:@"filename" forHTTPHeaderField:paraments[@"filename"]];
            [request setHTTPBody:paraments[@"data"]];
            [[self dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error)
              {
                  requestSuccess(responseObject,error);
              }] resume];
        }
    }
}


@end
